﻿using Hr.IDAL;
using Hr.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;


namespace Hr.DAL
{
    public class WageDAL
    {
        // 创建EF对象
        private DbContext db = DbContextFactory.Create();

        //核心dal层代码
        /// <summary>
        /// 查询所有薪资信息集合
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Wage_List> GetAttendceListAll(DateTime SelTime)
        {
            int Y = SelTime.Year;
            int M = SelTime.Month;
            string dt = Y + "/" + (M + 1) + "/1";
            DateTime EndTime = DateTime.Parse(dt);
            //条件：根据月份查早薪资，
            return (from Wage in db.Set<Wage_List>()
                    where Wage.EndDate<EndTime &
                   Wage.StartDate >= SelTime
                    select Wage
                );
        }
        /// <summary>
        /// 根据FullName查询Wage_List表数据
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Wage_List> FNameSelectWageList(string FullName)
        {
            return db.Set<Wage_List>().Where(n => n.FullName.Contains(FullName)||n.DepartmentName.Contains(FullName)).ToList();
        }
        /// <summary>
        /// 根据薪资ID，获取薪资信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Wage_List GetWageInfo(int id)
        {
            return db.Set<Wage_List>().SingleOrDefault(n => n.pk_Wage == id);
        }
        /// <summary>
        /// 根据薪资ID去查询
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public List<Wage_List> SelWageList(string WageList_CHS)
        {
            return db.Set<Wage_List>().Where(n => n.FullName.Contains(WageList_CHS)).ToList();
        }
        /// <summary>
        /// 获取当年当月的薪资
        /// </summary>
        /// <returns></returns>
        public List<Dictionary<string, string>> GetChartInfo()
        {
            List<Dictionary<string, string>> CharList = new List<Dictionary<string, string>>();
            //获取当年的所有数据，包括当年12个月
            Wage_List w = db.Set<Wage_List>().First();
            List<Wage_List> WageList = db.Set<Wage_List>().ToList();
            for (int i = 1; i <=12; i++)
            {
                double PriceCount = 0;//本月所有员工总工资
                //循环查找通过月份查找
                foreach (Wage_List item in WageList)
                {
                   int M= Convert.ToDateTime(item.StartDate.ToString().Split(' ')[0]).Month;//获取员工工资的月份
                   int Y= Convert.ToDateTime(item.StartDate.ToString().Split(' ')[0]).Year;
                    if (Y==DateTime.Now.Year&&M == i)
                    {
                        PriceCount +=double.Parse(item.TotalWage.ToString());
                    }
                }
                //格式化
                Dictionary<string, string> dt = new Dictionary<string, string>()
                {
                    {"时间",DateTime.Now.Year+"/"+i },
                    {"薪资",PriceCount.ToString()}
                };
                CharList.Add(dt);
            }
            return CharList;
        }
        /// <summary>
        /// 获取上年上月的薪资
        /// </summary>
        /// <returns></returns>
        public List<Dictionary<string, string>> GetChart()
        {
            List<Dictionary<string, string>> CharList = new List<Dictionary<string, string>>();
            //获取当年的所有数据，包括当年12个月
            Wage_List w = db.Set<Wage_List>().First();
            List<Wage_List> WageList = db.Set<Wage_List>().ToList();
            for (int i = 1; i <= 12; i++)
            {
                double PriceCount = 0;//本月所有员工总工资
                //循环查找通过月份查找
                foreach (Wage_List item in WageList)
                {
                    int M = Convert.ToDateTime(item.StartDate.ToString().Split(' ')[0]).Month;//获取员工工资的月份
                    int Y = Convert.ToDateTime(item.StartDate.ToString().Split(' ')[0]).Year;
                    if (Y == DateTime.Now.Year-1 && M == i)
                    {
                        PriceCount += double.Parse(item.TotalWage.ToString());
                    }
                }
                //格式化
                Dictionary<string, string> dt = new Dictionary<string, string>()
                {
                    {"时间",(DateTime.Now.Year-1)+"/"+i },
                    {"薪资",PriceCount.ToString()}
                };
                CharList.Add(dt);
            }
            return CharList;
        }
        /// <summary>
        /// 考勤管理的增加，编辑
        /// </summary>
        /// <returns></returns>
        public void UpdateWageList(Wage_List r, int pk_User,string type)
        {
            if (type.Equals("Add"))
            {
                r.CreateDate = DateTime.Now;//创建时间就为当前时间
                r.fk_User = pk_User;
                r.fk_Department = db.Set<User_List>().Where(n => n.pk_User == pk_User).FirstOrDefault().fk_Department;
                db.Set<Wage_List>().Add(r);
            }
            else if (type.Equals("Edit"))
            {
                
                ////先查询、在修改
                //Wage_List w = db.Set<Wage_List>().Where(n => n.pk_Wage == r.pk_Wage).FirstOrDefault();
                //修改人
                r.ModifyUser = pk_User;
                //修改时间
                r.ModifyDate = DateTime.Now;
                db.Set<Wage_List>().AddOrUpdate(r);
            }
        }
        /// <summary>
        /// 删除薪资信息
        /// </summary>
        /// <param name="r"></param>
        public void DelWagedata(Wage_List r)
        {
            db.Set<Wage_List>().Remove(r);
        }
        //提交
        public bool SaveChanges()
        {
            return db.SaveChanges() > 0;
        }
    }
}
